﻿
using Dapper;
using hdb;
using hentity;
using hlog;
using hutil;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace hwms {
    /**
   *┌────────────────────────────────────────────────┐
   *│　描    述：Component                                                    
   *│　作    者：sgj                                              
   *│　版    本：1.0                                              
   *│　创建时间：2020/9/2 11:28:16                        
   *└────────────────────────────────────────────────┘
   **/
    public abstract class CsdUtil {
        public const String baseCSDSql = "update core_stock_detail set change_time=sgj_get_sys_now, store_Status=";

        public static String format(string str ,params object[] a) {
            String sql = baseCSDSql + str;
            String res = HUtil.formatInt(sql ,a);
            return res;
        }
       

     
     
      


        static String where = String.Format("where store_Status={0} and stock_Id=@stockId " ,(int)StockDetailStatus.移库中);
        public static void taskDeleteUpdateStockDetail(IDbConnection conn ,IDbTransaction trans ,CoreTask ct) {//删除时
            if( LocConfig.isLoc(ct.src )) {
                string inStoreCsdSql = String.Format(baseCSDSql + "{0},r_loc_Id=@loc {1} " ,( int ) StockDetailStatus.库存 ,where);
                GkTransaction.exec(conn ,trans ,inStoreCsdSql ,new {loc= ct.src });
            }else {
                string deleteInSql = String.Format(baseCSDSql + "{0},r_loc_Id=@loc  {1}" ,( int ) StockDetailStatus.删除 ,where);
                GkTransaction.exec(conn ,trans ,deleteInSql  ,new { loc = ct.src });
            }
        }
        static public void finshStockDetail(IDbConnection conn ,IDbTransaction trans ,CoreTask ct ,int loc ,StockDetailStatus status) {//完成成时
            if( LocConfig.isLoc(loc ) ) {

                if( LocConfig.isLoc(ct.src ) ) {
                    string finshIningSql = String.Format(baseCSDSql + "{0},r_loc_Id=@loc,in_Time=sgj_get_sys_now  {1}  " ,( int ) StockDetailStatus.库存 ,where);
                    GkTransaction.exec(conn ,trans ,finshIningSql ,new {
                        ct.stockId ,
                        loc
                    });
                } else {
                    string finshIningSql = String.Format(baseCSDSql + "{0},r_loc_Id=@loc   {1}  " ,( int ) StockDetailStatus.库存 ,where);
                    GkTransaction.exec(conn ,trans ,finshIningSql ,new {
                        ct.stockId ,
                        loc
                    });
                } 
            } else {
                string finshOutingSql = baseCSDSql + "@status,r_loc_Id=@loc,out_Time=sgj_get_sys_now    " + where;
                GkTransaction.exec(conn ,trans ,finshOutingSql ,new {
                    ct.stockId ,
                    status = ( int ) status ,
                    loc
                });
            }
        }

        static public List<CSD> GetStockDetailList<CSD>(IDbConnection Connection ,IDbTransaction transaction ,long id) {
            string sql = @"select * from core_stock_detail where stock_Id=@id and store_Status>0";
            return Connection.Query<CSD>(sql ,new {
                id = id
            } ,transaction).ToList();
        }

      
        static public bool UpdateStockDetail(IDbConnection Connection ,IDbTransaction transaction ,long stockId ) {
            string sql = format("{0} where stock_Id=@stockId  and  store_Status>0" ,StockDetailStatus.移库中);
            int count = Connection.Execute(sql ,new { stockId = stockId  } ,transaction);
            return count == 1;
        }
       


       

     

        static public bool pkStockDetail(IDbConnection Connection ,IDbTransaction transaction ,long id ,long pkId) {//出库时
            string outingCSDSql = format("{0},PK_ID=@pkId where  store_Status={1} and stock_Id=@id" ,StockDetailStatus.待盘库 ,StockDetailStatus.库存);
            int count = Connection.Execute(outingCSDSql ,new { id , pkId } ,transaction);
            return true;
        }

        static public long getItemId<CSD>(List<CSD> stockDetailsList) where CSD : AbsCoreStockDetail, new() {
            long itemId = 0;
            foreach( CSD csd in stockDetailsList ) {
                if( itemId != 0 && itemId != csd.itemId ) {
                    throw new Exception(stockDetailsList[0].stockId + " 该托盘只能存放一个物料!");
                }
                itemId = csd.itemId;

            }
            return itemId;
        }
    }
}
